package view;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ComponentAdapter;
import utilities.Add;
import utilities.Delete;
import utilities.Edit;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import database.DBObject;

public class LOWVview extends JFrame {

	private JPanel contentPane;
	private JTable table_1;
	private JTable table;
	private DefaultTableModel aModel;
	private JScrollPane pane;
	private String currentTable;
	private Edit edit;
	private JComboBox reportBox;
	private String year;
	private boolean add = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LOWVview frame = new LOWVview();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws SQLException
	 */
	public LOWVview() throws SQLException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 980, 807);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		edit = new Edit();

		String action[] = { "add", "delete",
				"reset fiscal year" };

		JButton btnOk = new JButton("ok");
		JButton btnSubmit = new JButton("submit");
		btnSubmit.setSize(70, 24);
		btnSubmit.setLocation(189, 55);

		final JComboBox actionComboBox = new JComboBox(
				action);
		actionComboBox.setBounds(6, 54, 94, 27);
		contentPane.add(actionComboBox);

		contentPane.add(btnOk);
		contentPane.add(btnSubmit);

		btnOk.setBounds(112, 53, 75, 29);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if (actionComboBox.getSelectedItem()
						.equals("add")) {
					add = true;

					aModel.addRow(new Object[table
							.getColumnCount()]);
					table.addComponentListener(new ComponentAdapter() {
						public void componentResized(
								ComponentEvent e) {
							table.scrollRectToVisible(table.getCellRect(
									aModel.getRowCount() - 1,
									0, true));
						}
					});

				} else if (actionComboBox.getSelectedItem()
						.equals("reset fiscal year")) {
					try {
						DBObject myDB = new DBObject();
						myDB.resetFiscalYear();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if (actionComboBox.getSelectedItem()
						.equals("delete")) {
					delete();
				}
			}
		});

		btnSubmit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				add = false;
				int size = table.getColumnCount();
				String values[] = new String[size];
				int last = table.getRowCount() - 1;

				if (table.getCellEditor() != null) {
					table.getCellEditor().stopCellEditing();
				}

				for (int i = 0; i < size; i++) {
					if (table.getValueAt(last, i) == null) {
						values[i] = "";
					} else {
						values[i] = table.getValueAt(last,
								i).toString();
					}
				}

				Add add = null;
				try {
					add = new Add();
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				if (reportBox.getSelectedItem().equals(
						"Members")) {
					String[] temp = new String[7];
					temp[0] = values[8];
					temp[1] = values[2];
					temp[2] = values[3];
					temp[3] = values[4];
					temp[4] = values[5];
					temp[5] = values[6];
					temp[6] = values[7];

					try {
						add.addToTable(temp, "Contributors");
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					temp = new String[3];
					temp[0] = values[8];
					temp[1] = values[1];
					temp[2] = values[0];

					try {
						add.addToTable(temp, "Members");
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					temp = new String[2];
					temp[0] = values[10];
					temp[1] = values[8];

					try {
						add.addToTable(temp,
								"Hasmembership");
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else if (reportBox.getSelectedItem()
						.equals("Nonmembers")) {
					String[] temp = new String[7];
					temp[0] = values[7];
					temp[1] = values[1];
					temp[2] = values[2];
					temp[3] = values[3];
					temp[4] = values[4];
					temp[5] = values[5];
					temp[6] = values[6];

					try {
						add.addToTable(temp, "Contributors");
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					temp = new String[2];
					temp[0] = values[7];
					temp[1] = values[0];

					try {
						add.addToTable(temp, "Nonmembers");
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				} else if (reportBox.getSelectedItem()
						.equals("Transactions"))
					add.addTransaction(values);
				else if (reportBox.getSelectedItem()
						.equals("Categories")) {
					try {
						add.addSubcategory(
								values[0].toString(),
								values[1].toString(),
								values[2].toString());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					try {
						add.addToTable(values, reportBox
								.getSelectedItem()
								.toString());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			}
		});

		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Action");
		lblNewLabel.setBounds(34, 26, 41, 16);
		lblNewLabel
				.setHorizontalAlignment(SwingConstants.LEFT);
		contentPane.add(lblNewLabel);

		table = new JTable(/* data, columns */);
		pane = new JScrollPane(table);
		pane.setBounds(17, 130, 942, 643);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		contentPane.add(pane);

		table_1 = new JTable();
		table_1.setBounds(920, 225, 0, 0);
		contentPane.add(table_1);

		JLabel lblViewReport = new JLabel("Reports");
		lblViewReport.setBounds(464, 26, 110, 16);
		contentPane.add(lblViewReport);

		String report[] = { "Accounts", "Budget",
				"Categories", "Members",
				"Member Donations", "Monthly Breakdown",
				"Membership Types", "Nonmembers",
				"Transactions", "Vendors", "Yearly Budget" };
		reportBox = new JComboBox(report);
		reportBox.setBounds(339, 54, 128, 27);
		contentPane.add(reportBox);

		JButton showButton = new JButton("Show");
		showButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				displayTable();
			}
		});

		showButton.setBounds(479, 53, 75, 29);
		contentPane.add(showButton);

		JButton btnSaveAsPdf = new JButton("Save As PDF");
		btnSaveAsPdf
				.addActionListener(new ActionListener() {
					public void actionPerformed(
							ActionEvent arg0) {
						createPdf();
					}
				});

		table.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent me) {

				String value;
				if (add == false) {

					if (currentTable.equals("Transactions")) {

					} else if (currentTable
							.equals("Members")
							|| currentTable
									.equals("Nonmembers")) {

						value = JOptionPane
								.showInputDialog(null,
										"Enter Cell Value:");
						if (value != null) {
							String original, newValue, column, email;
							if (currentTable
									.equals("Nonmembers"))
								email = (String) table.getValueAt(
										table.getSelectedRow(),
										7);
							else
								email = (String) table.getValueAt(
										table.getSelectedRow(),
										8);

							column = table.getColumnName(table
									.getSelectedColumn());
							original = (String) table.getValueAt(
									table.getSelectedRow(),
									table.getSelectedColumn());
							table.setValueAt(
									value,
									table.getSelectedRow(),
									table.getSelectedColumn());
							newValue = table.getValueAt(
									table.getSelectedRow(),
									table.getSelectedColumn())
									+ "";

							try {
								edit.editPeople(
										original,
										currentTable
												.toString(),
										column, newValue,
										email);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}
					} else if (currentTable
							.equals("Vendors")) {
						value = JOptionPane
								.showInputDialog(null,
										"Enter Cell Value:");
						if (value != null) {
							String newValue, column, email;
							email = (String) table.getValueAt(
									table.getSelectedRow(),
									0);
							column = table.getColumnName(table
									.getSelectedColumn());
							table.setValueAt(
									value,
									table.getSelectedRow(),
									table.getSelectedColumn());
							newValue = table.getValueAt(
									table.getSelectedRow(),
									table.getSelectedColumn())
									+ "";
							try {
								edit.editVendors(column,
										newValue, email);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					} else if (currentTable
							.equals("Yearly Budget")) {

						if (table
								.getColumnName(
										table.getSelectedColumn())
								.equals("CURRENTYEARAMOUNT")) {
							value = JOptionPane
									.showInputDialog(null,
											"Enter Cell Value:");
							if (value != null) {
								String subcategory, amount, column;
								subcategory = (String) table.getValueAt(
										table.getSelectedRow(),
										0);
								table.setValueAt(
										value,
										table.getSelectedRow(),
										table.getSelectedColumn());
								amount = table.getValueAt(
										table.getSelectedRow(),
										table.getSelectedColumn())
										+ "";
								column = table
										.getColumnName(table
												.getSelectedColumn());

								try {
									edit.editSubcategory(
											subcategory,
											column, amount);
								} catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						} else if (table.getColumnName(
								table.getSelectedColumn())
								.equals("LOBBYINGEXP")) {
							value = JOptionPane
									.showInputDialog(null,
											"Enter Cell Value:");
							if (value != null) {
								String subcategory = (String) table.getValueAt(
										table.getSelectedRow(),
										0);
								table.setValueAt(
										value,
										table.getSelectedRow(),
										table.getSelectedColumn());
								String lobbyingexp = table.getValueAt(
										table.getSelectedRow(),
										table.getSelectedColumn())
										+ "";
								String column = column = table
										.getColumnName(table
												.getSelectedColumn());
								try {
									edit.editSubcategory(
											subcategory,
											column,
											lobbyingexp);
								} catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}

						}

					} else if (currentTable
							.equals("Budget")) {
						value = JOptionPane
								.showInputDialog(null,
										"Enter Cell Value:");
						if (value != null) {
							String oldV = null, newV, column;
							try {
								oldV = table
										.getValueAt(
												table.getSelectedRow(),
												0)
										.toString();
							} catch (Exception e) {
								if (oldV == null) {
									oldV = table
											.getValueAt(
													table.getSelectedRow(),
													1)
											.toString();
								}
							}
							table.setValueAt(
									value,
									table.getSelectedRow(),
									table.getSelectedColumn());
							newV = table.getValueAt(
									table.getSelectedRow(),
									table.getSelectedColumn())
									+ "";
							column = table.getColumnName(table
									.getSelectedColumn());

							try {
								edit.editBudget(column,
										newV, oldV);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					} else if (currentTable
							.equals("Membership Types")) {
						value = JOptionPane
								.showInputDialog(null,
										"Enter Cell Value:");
						if (value != null) {
							String type, amount, column;
							type = (String) table.getValueAt(
									table.getSelectedRow(),
									0);
							table.setValueAt(
									value,
									table.getSelectedRow(),
									table.getSelectedColumn());
							amount = table.getValueAt(
									table.getSelectedRow(),
									table.getSelectedColumn())
									+ "";
							column = table.getColumnName(table
									.getSelectedColumn());

							try {
								edit.editMembershipTypes(
										column, amount,
										type);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		});

		btnSaveAsPdf.setBounds(553, 53, 117, 29);
		contentPane.add(btnSaveAsPdf);

	}

	protected void delete() {

		if (currentTable.equalsIgnoreCase("Transactions")) {
			String value = JOptionPane.showInputDialog(
					null, "Enter ID of Transaction:");
			if (value != null) {
				Delete d = null;
				try {
					d = new Delete();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				d.deleteTransaction(value);
				displayTable();
			}
		} else if (currentTable.equalsIgnoreCase("Members")) {
			String value = JOptionPane.showInputDialog(
					null,
					"Enter First and Last Name of Member:");
			if (value != null) {
				String[] split = value.split(" ");
				Delete d = null;
				try {
					d = new Delete();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				d.deleteMember(split[0], split[1]);
				displayTable();
			}
		} else if (currentTable
				.equalsIgnoreCase("Accounts")) {
			String value = JOptionPane.showInputDialog(
					null, "Enter acount number:");
			if (value != null) {
				Delete d = null;
				try {
					d = new Delete();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				d.deleteAccount(value);
				displayTable();
			}
		} else if (currentTable
				.equalsIgnoreCase("Categories")) {
			String value = JOptionPane.showInputDialog(
					null, "Enter name:");
			if (value != null) {
				Delete d = null;
				try {
					d = new Delete();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				d.deleteSubCategory(value);
				displayTable();
			}
		}

		else if (currentTable
				.equalsIgnoreCase("Nonmembers")) {
			String value = JOptionPane.showInputDialog(
					null, "Enter name:");
			if (value != null) {
				Delete d = null;
				try {
					d = new Delete();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				d.deleteNonMember(value);
				displayTable();
			}
		} else if (currentTable.equalsIgnoreCase("Vendors")) {
			String value = JOptionPane.showInputDialog(
					null, "Enter name:");
			if (value != null) {
				Delete d = null;
				try {
					d = new Delete();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				d.deleteVendor(value);
				displayTable();
			}
		}

	}

	protected void displayTable() {

		ResultSet mySet = null;
		DBObject getter = null;
		try {
			getter = new DBObject();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		currentTable = (String) reportBox.getSelectedItem();

		if (currentTable.equals("Member Donations")) {

			year = JOptionPane
					.showInputDialog("Enter Calendar Year:");
			System.out.println(year);
			try {
				mySet = getter.getTable(currentTable, year);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (currentTable.equals("Monthly Breakdown")) {
			year = JOptionPane
					.showInputDialog("Enter Fiscal Year:");

			try {
				mySet = getter.getTable(currentTable, year);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (currentTable.equals("Transactions")) {
			year = JOptionPane
					.showInputDialog("Enter Fiscal Year:");
			try {
				mySet = getter.getTable(currentTable, year);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			try {
				mySet = getter.getTable(currentTable);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		Object[] objects = null;
		// Loop through the ResultSet and transfer in the Model
		double overalltotalBudget = 0.0;
		double overalltotalActual = 0.0;
		double overalltotalActualAs = 0.0;
		double overalltotalLobbying = 0.0;
		double overalltotalPrevious = 0.0;
		try {
			aModel = new DefaultTableModel();
			if (currentTable.equals("Yearly Budget")) {
				double totalBudget = 0.0;
				double totalActual = 0.0;
				double totalActualAs = 0.0;
				double totalLobbying = 0.0;
				double totalPrevious = 0.0;
				java.sql.ResultSetMetaData rsmd = mySet
						.getMetaData();
				int colNo = rsmd.getColumnCount();
				String[] columnNames = new String[colNo];

				// Get the column names; column indices start from 1
				for (int i = 1; i < colNo + 1; i++) {
					columnNames[i - 1] = rsmd
							.getColumnName(i);

					// Get the name of the column's table name
					String tableName = rsmd.getTableName(i);
				}
				aModel.setColumnIdentifiers(columnNames);
				String currentCategory = " ";
				boolean first = false;
				int counter = 1;
				while (mySet.next()) {
					objects = new Object[colNo];

					Double lobbying = null;
					for (int i = 0; i < colNo; i++) {
						if (i == 0) {
							if (currentCategory
									.equals(mySet
											.getObject(i + 1)))
								objects[i] = null;
							else {
								if (first) {

									Object[] t = new Object[8];
									t[0] = "         Total "
											+ currentCategory;
									t[1] = null;
									t[2] = null;
									t[3] = totalPrevious;
									t[4] = totalBudget;
									t[5] = totalActual;
									t[6] = totalLobbying;
									t[7] = totalActualAs;
									overalltotalBudget = totalBudget
											+ overalltotalBudget;
									overalltotalActual = overalltotalActual
											+ totalActual;
									overalltotalActualAs = overalltotalActualAs
											+ totalActualAs;
									overalltotalLobbying = overalltotalLobbying
											+ totalLobbying;
									overalltotalPrevious = overalltotalPrevious
											+ totalPrevious;
									aModel.addRow(t);
									Object[] q = new Object[8];
									aModel.addRow(q);

									currentCategory = (String) mySet
											.getObject(i + 1);
									Object[] cat = new Object[8];
									cat[0] = counter
											+ ". "
											+ mySet.getObject(i + 1);
									counter++;
									cat[2] = mySet
											.getObject(i + 3);
									aModel.addRow(cat);
									totalBudget = 0.0;
									totalActual = 0.0;
									totalActualAs = 0.0;
									totalLobbying = 0.0;
									totalPrevious = 0.0;
								} else {

									Object[] cat = new Object[8];
									cat[0] = counter
											+ ". "
											+ mySet.getObject(i + 1);
									counter++;
									cat[2] = mySet
											.getObject(i + 3);
									aModel.addRow(cat);
									currentCategory = (String) mySet
											.getObject(i + 1);

								}
							}

						} else if (i == 6) {

							if (mySet.getObject(i) != null) {

								Double temp2 = ((Double) (mySet
										.getObject(i)));
								if (((String) mySet
										.getObject(i + 1))
										.equals("T")) {

									Double t = temp2 * .07;
									double value = t
											.doubleValue();
									long factor = (long) Math
											.pow(10, 2);
									value = value * factor;
									long tmp = Math
											.round(value);

									objects[i] = (double) tmp
											/ factor;
									lobbying = ((double) tmp / factor)
											+ temp2.doubleValue();
									totalLobbying = ((double) tmp / factor)
											+ totalLobbying;

								} else {
									objects[i] = null;
									lobbying = temp2
											.doubleValue();

								}
							}
						} else if (i == (colNo - 1)) {
							objects[i] = lobbying;
							if (lobbying != null)
								totalActualAs = lobbying
										+ totalActualAs;
						} else if (i == 2) {
							objects[i] = null;
						} else {

							objects[i] = mySet
									.getObject(i + 1);
							if (i == 3
									&& objects[i] != null) {
								totalPrevious = totalPrevious
										+ (Double) objects[i];
							} else if (i == 4
									&& objects[i] != null) {
								totalBudget = totalBudget
										+ (Double) objects[i];
							} else if (i == 5
									&& objects[i] != null)
								totalActual = totalActual
										+ (Double) objects[i];
						}

						first = true;
					}

					aModel.addRow(objects);
				}
				Object[] t = new Object[8];
				t[0] = "         Total Expenses ";
				t[1] = null;
				t[2] = null;
				t[3] = overalltotalPrevious;
				t[4] = overalltotalBudget;
				t[5] = overalltotalActual;
				t[6] = overalltotalLobbying;
				t[7] = overalltotalActualAs;
				aModel.addRow(t);
			}

			else if (currentTable
					.equals("Monthly Breakdown")) {
				java.sql.ResultSetMetaData rsmd = mySet
						.getMetaData();
				int colNo = rsmd.getColumnCount();
				String[] columnNames = new String[colNo];
				double july = 0, august = 0, september = 0, october = 0, november = 0, december = 0, january = 0, feburary = 0, march = 0, april = 0, may = 0, june = 0;

				// Get the column names; column indices start from 1
				for (int i = 1; i < colNo + 1; i++) {
					columnNames[i - 1] = rsmd
							.getColumnName(i);

					// Get the name of the column's table name
					String tableName = rsmd.getTableName(i);
				}
				aModel.setColumnIdentifiers(columnNames);
				while (mySet.next()) {
					objects = new Object[colNo];
					if (mySet.getObject(2) != null) {
						for (int i = 0; i < colNo; i++) {
							objects[i] = mySet
									.getObject(i + 1);
							if (i == 1
									&& objects[i] != null) {
								july += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 2
									&& objects[i] != null) {
								august += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 3
									&& objects[i] != null) {
								september += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 4
									&& objects[i] != null) {
								october += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 5
									&& objects[i] != null) {
								november += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 6
									&& objects[i] != null) {
								december += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 7
									&& objects[i] != null) {
								january += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 8
									&& objects[i] != null) {
								feburary += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 9
									&& objects[i] != null) {
								march += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 10
									&& objects[i] != null) {
								april += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 11
									&& objects[i] != null) {
								may += Double
										.parseDouble(objects[i]
												.toString());
							} else if (i == 12
									&& objects[i] != null) {
								june += Double
										.parseDouble(objects[i]
												.toString());
							}

						}

						aModel.addRow(objects);
					}

				}
				Object[] o = { "Total Income: ", july,
						august, september, october,
						november, december, january,
						feburary, march, april, may, june };
				aModel.addRow(o);
				o = new Object[colNo];
				aModel.addRow(o);
				mySet = getter.getTable("Expenses", year);
				double july1 = 0, august1 = 0, september1 = 0, october1 = 0, november1 = 0, december1 = 0, january1 = 0, feburary1 = 0, march1 = 0, april1 = 0, may1 = 0, june1 = 0;
				while (mySet.next()) {
					objects = new Object[colNo];
					if (mySet.getObject(2) != null) {
						for (int i = 0; i < colNo; i++) {
							objects[i] = mySet
									.getObject(i + 1);
							if (i == 1
									&& objects[i] != null) {
								july1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 2
									&& objects[i] != null) {
								august1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 3
									&& objects[i] != null) {
								september1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 4
									&& objects[i] != null) {
								october1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 5
									&& objects[i] != null) {
								november1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 6
									&& objects[i] != null) {
								december1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 7
									&& objects[i] != null) {
								january1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 8
									&& objects[i] != null) {
								feburary1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 9
									&& objects[i] != null) {
								march1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 10
									&& objects[i] != null) {
								april1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 11
									&& objects[i] != null) {
								may1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							} else if (i == 12
									&& objects[i] != null) {
								june1 += Double
										.parseDouble(objects[i]
												.toString())
										* -1;
								objects[i] = Double
										.parseDouble(mySet
												.getObject(
														i + 1)
												.toString())
										* -1;
							}

						}

						aModel.addRow(objects);
					}

				}
				Object[] t = { "Total Expenses: ", july1,
						august1, september1, october1,
						november1, december1, january1,
						feburary1, march1, april1, may1,
						june1 };
				aModel.addRow(t);

			}

			else {
				java.sql.ResultSetMetaData rsmd = mySet
						.getMetaData();
				int colNo = rsmd.getColumnCount();
				String[] columnNames = new String[colNo];

				// Get the column names; column indices start from 1
				for (int i = 1; i < colNo + 1; i++) {
					columnNames[i - 1] = rsmd
							.getColumnName(i);

					// Get the name of the column's table name
					String tableName = rsmd.getTableName(i);
				}
				aModel.setColumnIdentifiers(columnNames);
				while (mySet.next()) {
					objects = new Object[colNo];
					for (int i = 0; i < colNo; i++) {
						objects[i] = mySet.getObject(i + 1);
					}

					aModel.addRow(objects);
				}
			}
			table = autoResizeColWidth(table, aModel);

			table.setModel(aModel);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings("deprecation")
	public void createPdf() {
		Document document = new Document();
		try {
			PdfWriter writer;

			writer = PdfWriter.getInstance(document,
					new FileOutputStream("mytable.pdf"));
			document.open();
			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate tp = cb.createTemplate(500, 500);
			Graphics2D g2;

			g2 = tp.createGraphicsShapes(500, 500);
			pane.printAll(g2);
			g2.dispose();
			cb.addTemplate(tp, 30, 300);
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		document.close();

	}

	public JTable autoResizeColWidth(JTable table,
			DefaultTableModel model) {
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setModel(model);

		int margin = 5;

		for (int i = 0; i < table.getColumnCount(); i++) {
			int vColIndex = i;
			DefaultTableColumnModel colModel = (DefaultTableColumnModel) table
					.getColumnModel();
			TableColumn col = colModel.getColumn(vColIndex);
			int width = 0;

			// Get width of column header
			TableCellRenderer renderer = col
					.getHeaderRenderer();

			if (renderer == null) {
				renderer = table.getTableHeader()
						.getDefaultRenderer();
			}

			Component comp = renderer
					.getTableCellRendererComponent(table,
							col.getHeaderValue(), false,
							false, 0, 0);

			width = comp.getPreferredSize().width;

			// Get maximum width of column data
			for (int r = 0; r < table.getRowCount(); r++) {
				renderer = table.getCellRenderer(r,
						vColIndex);
				comp = renderer
						.getTableCellRendererComponent(
								table, table.getValueAt(r,
										vColIndex), false,
								false, r, vColIndex);
				width = Math.max(width,
						comp.getPreferredSize().width);
			}

			// Add margin
			width += 2 * margin;

			// Set the width
			col.setPreferredWidth(width);
		}

		((DefaultTableCellRenderer) table.getTableHeader()
				.getDefaultRenderer())
				.setHorizontalAlignment(SwingConstants.LEFT);

		// table.setAutoCreateRowSorter(true);
		table.getTableHeader().setReorderingAllowed(false);

		return table;
	}
}
